//software.js

var util = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    feed: [],
    feed_length: 0,
    feed_page:1
  },
  onLoad: function () {
    var that = this
    //获取全局数据
    that.getData();
  },
  upper: function () {
    wx.showNavigationBarLoading()
    this.refresh();
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000);
  },
  lower: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function(){wx.hideNavigationBarLoading();that.nextLoad();}, 1000);
  },
  //scroll: function (e) {
  //  console.log("scroll")
  //},
  //转发
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '软件资讯',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //获取最新数据
  getData: function(){
    var that = this
    wx.request({
      url: 'https://www.oschina.net/action/ajax/get_more_news_list?newsType=project&p=' + that.data.feed_page,
      success: function (res) {
        if(res.statusCode==200){
          if (res.data.length > 0) {
            var data = util.formatReadHub(res.data);
            that.setData({
              feed: data,
              feed_length: data.length,
              feed_page: that.data.feed_page + 1
            });
          }
        }else{
          console.debug(res);
        }
      },
      fail: function (res) {
        console.log("failed");
        util.toastNetError();
      }
    })
  },

  refresh: function(){
    var that =this;
    that.setData({feed_page: 1});
    wx.showToast({
      title: '刷新中',
      icon: 'loading',
      duration: 1500
    });
    that.getData();
    setTimeout(function(){
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1500
      })
    },2000)

  },

  //使用本地 fake 数据实现继续加载效果
  nextLoad: function(){
    var that =this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 500
    })
    var lastItem = that.data.feed[that.data.feed_length-1];
    wx.request({
      url: 'https://www.oschina.net/action/ajax/get_more_news_list?newsType=project&p=' + that.data.feed_page,
      success: function (res) {
        if (res.statusCode == 200) {
          if (res.data.length > 0) {
            try{
              var formatData = util.formatReadHub(res.data);
            }catch(e){
              setTimeout(function () {
                wx.showToast({
                  title: '数据出错，跳过一页',
                  icon: 'success',
                  duration: 500
                })
              }, 500);
              that.setData({
                feed_page: that.data.feed_page + 1
              });
              return ;
            }
            var data = that.data.feed.concat(formatData);
            that.setData({
              feed: data,
              feed_length: data.length,
              feed_page: that.data.feed_page + 1
            });
            setTimeout(function () {
              wx.showToast({
                title: '加载成功',
                icon: 'success',
                duration: 500
              })
            }, 500);
          }
        } else {
          console.log(res);
        }
      },
      fail: function (res) {
        console.log("failed");
        util.toastNetError();
      }
    })
  },
  //跳转到详情页
  jumpDetail:function(e){
    var itemId = e.currentTarget.dataset.itemid;
    var that = this;
    var detail = that.data.feed[itemId];
    wx.setStorageSync('detail', detail);
    wx.navigateTo({
      url: '../detail/detail'
    })
  }


})
